home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.sprintlink.net!eskimo!scs
- From: scs@eskimo.com (Steve Summit)
- Subject: Re: mixing enums and integers
- X-Nntp-Posting-Host: eskimo.com
- Message-ID: <DKtpIL.72z@eskimo.com>
- Sender: news@eskimo.com (News User Id)
- Organization: schmorganization
- References: <4cij6k$dvb@yrkpa.kias.com>
- Date: Sun, 7 Jan 1996 18:18:20 GMT
-
- In article <4cij6k$dvb@yrkpa.kias.com>, glynis@yrkpa.kias.com
- (John Flinchbaugh) writes:
- > the faq states that mixing enums and ints is legal, but poor style.
-
- Hm. I can see how you might have interpreted it that way, but
- let me state for the record that that's not quite what the FAQ
- list is trying to say.
-
- The actual wording is that indiscriminate mixing of enumerations
- and integers "can still be considered bad style." If you believe
- that enumerations are an abstract type, distinct from plain
- integers, then you will not deliberately mix them in your code,
- and you will wish that correctness-checking tools would warn you
- when you do mix them (since under your style, such mixing is
- likely in error), and you may lament the ANSI C committee's
- decision that enumerations essentially *are* integers, thus
- blurring the distinction you'd like to observe. If, on the other
- hand, your programming style is not so discriminatory between
- enumerations and integers, there's no reason you can't mix them
- freely, and the historical implementation of enumerations by
- compilers (as codified by the Standard) agrees with you.
- Finally, the FAQ list isn't even trying to insinuate that one
- approach is better than the other. (I do tend to have my own
- opinions on these things, and I suppose I let them slip through
- in the FAQ list's wording more often than I ought to, but in this
- case, my opinion is not strong, and the issue isn't worth making
- a fuss over, so I really wasn't trying to polarize the discussion
- at all.)
-
- Note also the insertion of that modifier "indiscriminate" in the
- FAQ list's wording. Even if you do attempt to enforce a strict
- distinction between enumerations and integers in your code, the
- practical reality is that there are plenty of cases where an
- isolated assignment of an enumeration constant to an integer or
- an integer to an enumeration ends up being necessary. I think
- the example posted by John Flinchbaugh probably falls into that
- category.
-
- > would casting it be more proper:
- > if ((enum DIRECT) key==up) ...
-
- I don't think the cast buys you much stylistically, but it would
- tend to suppress warnings by compilers attempting to enforce a
- discipline which differentiates between enumerations and integers.
-
- > also, can you typedef enums? if so, how?
-
- Certainly.
-
- typedef enum {...} enum_t;
- or
- enum x {...};
- typedef enum x enum_t;
- or
- typedef enum x enum_t;
- enum x {...};
-
- Steve Summit
- scs@eskimo.com
-